<?php

namespace app\admin\model;

use think\Db;
use think\Model;

class Article extends Model
{
    protected $table = 'petct_article';
    protected $field = true;

    /**
     * 获取文章信息
     * @param $limit
     * @param $where
     * @return array
     */
    public function getArticle($limit, $where)
    {
        try {

//            $res = $this->where($where)->order('id', 'desc')->paginate($limit);
            $res = Db::name('article')
                ->alias('a')
                ->leftJoin('category b', 'a.catid = b.id')
                ->field('a.*,b.catname')
                ->where($where)
                ->order('a.id desc')
                ->paginate($limit);

        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }

    /**
     * 增加文章
     * @param $params
     * @return array
     */
    public function addArticle($params)
    {
        try {

            $where = [
                'title' => $params['title']
            ];
            $flag = $this->where($where)->count();
            if ($flag) {
                return modelReMsg(-2, '', '文章已经存在');
            }

            $params['add_time'] = date('Y-m-d H:i:s');
            $params['update_time'] = date('Y-m-d H:i:s');
            $params['userid'] = session('admin_user_id');
            $params['username'] = session('admin_user_name');
            $params['onclick'] = rand(100, 1000);

            $this->strict(false)->insert($params);
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '文章添加成功');
    }

    /**
     * 根据id获取栏目信息
     * @param $id
     * @return array
     */
    public function getArticleById($id)
    {
        try {

            $info = $this->where('id', $id)->findOrEmpty()->toArray();
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $info, 'ok');
    }

    /**
     * 编辑栏目
     * @param $params
     * @return array
     */
    public function editArticle($params)
    {
        try {

            $has = $this->where('title', $params['title'])
                ->where('id', '<>', $params['id'])
                ->findOrEmpty()->toArray();

            if (!empty($has)) {
                return modelReMsg(-2, '', '文章已经存在');
            }
            $params['update_time'] = date('Y-m-d H:i:s');

            $this->save($params, ['id' => $params['id']]);
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '编辑文章成功');
    }

    /**
     * 删除栏目
     * @param $id
     * @return array
     */
    public function delArticle($id)
    {
        try {

            $this->where('id', $id)->delete();
        } catch (\Exception $e) {

            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '删除成功');
    }
}